package model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.vecmath.Point3d;



import model.lights.Light;
import model.primitive.Primitive;
import model.ray.Ray;
import model.shader.Shader;
import camera.Camera;

public class Scene {
	public Image image;
	public Camera camera;
	public Collection<Shader> shaders;
	public Collection<Primitive> primitives;
	public Collection<Light> lights;
	public int bucket = 0;
	
	public Scene() {
		super();
		/* Cambiar por un tipo concreto de coleccion */
		this.lights = null;
		this.shaders = null;
		this.primitives = null;
		this.camera = null;
	}
	
	public Scene( Collection<Light> lights, Collection<Shader> shaders, 
			Collection<Primitive> primitives ) {
		if (lights == null || shaders == null || primitives == null) {
			throw new IllegalArgumentException("Some of the arguments are null");
		}
		this.lights = new ArrayList<Light>(); 
		this.shaders = new ArrayList<Shader>();
		this.primitives = new ArrayList<Primitive>();
		
		this.lights.addAll(lights);
		this.shaders.addAll(shaders);
		this.primitives.addAll(primitives);
	}
	
	public Scene( Collection<Primitive> primitives ) {
		this.primitives = new ArrayList<Primitive>();
		this.primitives.addAll(primitives);
	}

	public Collection<Light> getLights() {
		return lights;
	}

	public void setLights(Collection<Light> lights) {
		this.lights.clear();
		
		if (lights != null) {
			this.lights.addAll(lights);
		}
	}
	
	public boolean addLight(Light l) {
		if (l != null) {
			return this.lights.add(l);
		}
		
		return false;
	}

	public boolean addLights(Collection<Light> lights) {
		if (lights != null) {
			return this.lights.addAll(lights);
		}
		
		return false;
	}
	
	public boolean removeLight(Light l) {
		if (l != null) {
			return this.lights.remove(l);
		}
		
		return false;
	}
	
	public boolean removeLights(Collection<Light> lights) {
		if (lights != null) {
			return this.lights.removeAll(lights);
		}
		
		return false;
	}
	
	public Collection<Shader> getShaders() {
		return shaders;
	}

	public void setShaders(Collection<Shader> shaders) {
		this.shaders.clear();
		
		if (shaders != null) {
			this.shaders.addAll(shaders);
		}
		this.shaders = shaders;
	}
	
	public boolean addShader(Shader s) {
		if (s != null) {
			return this.shaders.add(s);
		}
		
		return false;
	}
	
	public boolean addShaders(Collection<Shader> shaders) {
		if (shaders != null) {
			return this.shaders.addAll(shaders);
		}
		
		return false;
	}

	public boolean removeShader(Shader s) {
		if (s != null) {
			return this.shaders.remove(s);
		}
		
		return false;
	}
	
	public boolean removeShaders(Collection<Shader> shaders) {
		if (shaders != null) {
			return this.shaders.removeAll(shaders);
		}
		
		return false;
	}
	
	public Collection<Primitive> getPrimitives() {
		return primitives;
	}
	
	public void setPrimitives(Collection<Primitive> primitives) {
		this.primitives.clear();
		
		if (primitives != null) {
			this.primitives.addAll(primitives);
		}
	}
	
	public boolean addPrimitive(Primitive p) {
		if (p != null) {
			return this.primitives.add(p);
		}
		
		return false;
	}
	
	public boolean addPrimitives(Collection<Primitive> primitives) {
		if (primitives != null) {
			return this.primitives.addAll(primitives);
		}
		
		return false;
	}
	
	public boolean removePrimitive(Primitive p) {
		if (p != null) {
			return this.primitives.remove(p);
		}
		
		return false;
	}
	
	public boolean removePrimitives(Collection<Primitive> primitives) {
		if (primitives != null) {
			return this.primitives.removeAll(primitives);
		}
		
		return false;
	}

	public Camera getCamera() {
		return camera;
	}
	
	public void setCamera(Camera cam) {
		this.camera = cam;
	}

	public Primitive findIntersection(Ray ray) {
		Point3d curr = null;
		Double dist, currdist;
		Primitive prim;
		
		dist = null;
		curr = null;
		prim = null;
		
		for( Primitive primitive :  primitives ) {
			List<Point3d> intersections = primitive.getIntersections(ray);
			if(intersections != null && intersections.size() > 0)	{
				curr = intersections.get(0);
			}
			
			if (dist == null && curr != null) {
				dist = ray.getDistance(curr);
				prim = primitive;
			} else {	
				if (curr != null) {
					currdist = ray.getDistance(curr);
					
					if (currdist < dist) {
						dist = currdist;
						prim = primitive;
					}
				}
			}
		}
		return prim;
	}

	public boolean isAAEnabled() {
		return image.aa.isAAEnabled();
	}

	public void setImage(Image image) {
		this.image = image;
	}
	
	public String toString() {
		StringBuffer buf = new StringBuffer();
		buf.append("Primitives:\n");
		Iterator<Primitive> it = primitives.iterator(); 
		while (it.hasNext()) {
			buf.append(it.next());
		}
		
		return buf.toString();
	}
	
}
